package nccu.model;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

/**
 * This class represents the basic "account_code" object that allows for account_code
 * persistence and management.
 *
 * @author <a href="mailto:chris.oberle@gmail.com">Chris Oberle</a>
 */
@Entity
@Table(name = "account_code")
@Searchable
@XmlRootElement
public class AccountCode extends BaseObject implements Serializable {
    private static final long serialVersionUID = 3832626162173359413L;

    private Long id;
    private String code; // required
    private String description;

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public AccountCode(){}

    /**
     * Create a new instance and set the code.
     *
     * @param code code for account_code.
     */
    public AccountCode(final String code) {
        this.code = code;
    }

    /**
     * Create a new instance and set the code and description.
     *
     * @param code for account_code.
     * @param description for account_code.
     */
    public AccountCode(final String code, final String description) {
        this.code = code;
        this.description = description;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @SearchableId
    @Column (name="account_id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(nullable = false, length = 10, unique = true)
    @SearchableProperty
    public String getCode() {
        return code;
    }

    public void setCode(String code){
        this.code = code;
    }

    @Column(nullable = true, length = 300, unique = false)
    @SearchableProperty
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AccountCode)) return false;

        AccountCode that = (AccountCode) o;

        if (!code.equals(that.code)) return false;
        if (!id.equals(that.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + code.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "AccountCode{" +
                "id=" + id +
                ", code='" + code + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}
